/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ticketmaster.presentation.customer;

/**
 *
 * @author Xiaolong
 */
import java.awt.*;
import java.util.*;
import java.text.*;

import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.*;
import ticketmaster.businessobject.RewardBO;

public class RewardTable extends JPanel
{
    private JTable m_table;
    private RewardTableData m_data;

    public RewardTable(Collection<RewardBO> rewards)
    {
        setSize(700, 400);

        m_data = new RewardTableData(rewards);

        //getContentPane().add(m_title, BorderLayout.NORTH);

        m_table = new JTable();
        m_table.setAutoCreateColumnsFromModel(false);
        m_table.setModel(m_data);
        m_table.setPreferredScrollableViewportSize(new Dimension(700, 400));
        m_table.setFillsViewportHeight(true);
        m_table.setRowHeight(50);
        m_table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        m_table.setFont(Font.getFont("Tahoma", new Font(Font.SANS_SERIF, Font.PLAIN, 14)));

        for (int k = 0; k < RewardTableData.m_columns.length; k++)
        {
            TableCellRenderer renderer;
            if (k == RewardTableData.COL_SELECTED)
            {
                renderer = new RewardCheckCellRenderer();
            }
            else
            {
                DefaultTableCellRenderer textRenderer =
                                         new DefaultTableCellRenderer();
                textRenderer.setHorizontalAlignment(
                        RewardTableData.m_columns[k].m_alignment);

                renderer = textRenderer;
            }

            TableCellEditor editor;

            if (k == RewardTableData.COL_SELECTED)
            {
                JCheckBox checkBox = new JCheckBox();
                checkBox.setHorizontalAlignment(JLabel.CENTER);
                editor = new DefaultCellEditor(checkBox);
            }
            else
            {
                editor = new DefaultCellEditor(new JTextField());
            }

            TableColumn column = new TableColumn(k,
                                                 RewardTableData.m_columns[k].m_width,
                                                 renderer, editor);
            m_table.addColumn(column);
        }

        JTableHeader header = m_table.getTableHeader();
        header.setUpdateTableInRealTime(false);

        JScrollPane ps = new JScrollPane();
        ps.getViewport().add(m_table);
        ps.setSize(700, 400);


        this.add(ps);
    }

    /**
     * @return the m_table
     */
    public JTable getTable()
    {
        return m_table;
    }

    /**
     * @param m_table the m_table to set
     */
    public void setTable(JTable m_table)
    {
        this.m_table = m_table;
    }
}

class RewardCheckCellRenderer extends JCheckBox implements TableCellRenderer
{

    protected static Border m_noFocusBorder;

    public RewardCheckCellRenderer()
    {
        super();
        m_noFocusBorder = new EmptyBorder(1, 2, 1, 2);
        setOpaque(true);
        setBorder(m_noFocusBorder);
        this.setHorizontalAlignment(this.CENTER);
    }

    public Component getTableCellRendererComponent(JTable table,
                                                   Object value, boolean isSelected, boolean hasFocus,
                                                   int row, int column)
    {
        if (value instanceof Boolean)
        {
            Boolean b = (Boolean) value;
            setSelected(b.booleanValue());
        }

        setBackground(isSelected && !hasFocus
                      ? table.getSelectionBackground() : table.getBackground());
        setForeground(isSelected && !hasFocus
                      ? table.getSelectionForeground() : table.getForeground());

        setFont(table.getFont());
        setBorder(hasFocus ? UIManager.getBorder(
                "Table.focusCellHighlightBorder") : m_noFocusBorder);

        return this;
    }
}

class RewardColumnData
{

    public String m_title;
    public int m_width;
    public int m_alignment;

    public RewardColumnData(String title, int width, int alignment)
    {
        m_title = title;
        m_width = width;
        m_alignment = alignment;
    }
}

class RewardTableData extends AbstractTableModel
{

    public static final int COL_SELECTED = 3;
    static final public RewardColumnData m_columns[] =
    {
        new RewardColumnData("Reward Name", 200, JLabel.CENTER),
        new RewardColumnData("Reward Price", 100, JLabel.CENTER),
        new RewardColumnData("Reward Points", 100, JLabel.CENTER),
        new RewardColumnData("Selected", 100, JLabel.CENTER)
    };
    protected SimpleDateFormat m_frm;
    private Vector m_vector;

    public RewardTableData(Collection<RewardBO> rewards)
    {
        m_frm = new SimpleDateFormat("MM/dd/yyyy");
        m_vector = new Vector();
        setData(rewards);
    }

    public void setData(Collection<RewardBO> rewards)
    {
        m_vector.removeAllElements();
        m_vector.addAll(rewards);
    }

    public int getRowCount()
    {
        return m_vector == null ? 0 : m_vector.size();
    }

    public int getColumnCount()
    {
        return m_columns.length;
    }

    public String getColumnName(int column)
    {
        return m_columns[column].m_title;
    }

    public Class getColumnClass(int c)
    {
        return getValueAt(0, c).getClass();
    }

    public boolean isCellEditable(int nRow, int nCol)
    {
        if (nCol < 3)
        {
            return false;
        }
        else
        {
            return true;
        }
    }

    public Object getValueAt(int nRow, int nCol)
    {
        if (nRow < 0 || nRow >= getRowCount())
        {
            return "";
        }
        RewardBO row = (RewardBO) m_vector.elementAt(nRow);
        switch (nCol)
        {
            case 0:
                return row.getName();
            case 1:
                return row.getPrice();
            case 2:
                return row.getPoints();
            case 3:
                return row.isSelected();
        }
        return "";
    }

    public void setValueAt(Object value, int nRow, int nCol)
    {
        if (nRow < 0 || nRow >= getRowCount())
        {
            return;
        }
        RewardBO row = (RewardBO) m_vector.elementAt(nRow);
        //String svalue = value.toString();

        switch (nCol)
        {
            case COL_SELECTED:
                row.setSelected((Boolean) value);
                break;
        }
    }

    public String getTitle()
    {
        return "Gift Cards Available";
    }

}